﻿namespace Algoritmia.Problems.Arithmetics
{
    public static class Arithmetics
    {
        public static double Power1(int a, int n)
        {
            if ( n > 2)
            {
                if (n %2 == 0)
                {
                    return Power1(a, n/2)*Power1(a, n/2);
                }
             
                return Power1(a, n/2)*Power1(a, n/2+1);
            }
            if ( n == 2)
            {
                return a*a;
            }
            if ( n == 1)
            {
                return a;
            }
            return 1;
        }

        public static decimal Power1(decimal a, int n)
        {
            if (n > 2)
            {
                if (n % 2 == 0)
                {
                    return Power1(a, n / 2) * Power1(a, n / 2);
                }

                return Power1(a, n / 2) * Power1(a, n / 2 + 1);
            }
            if (n == 2)
            {
                return a * a;
            }
            if (n == 1)
            {
                return a;
            }
            return 1;
        }

        public static double Power1(double a, int n)
        {
            if (n > 2)
            {
                if (n % 2 == 0)
                {
                    return Power1(a, n / 2) * Power1(a, n / 2);
                }

                return Power1(a, n / 2) * Power1(a, n / 2 + 1);
            }
            if (n == 2)
            {
                return a*a;
            }
            if (n == 1)
            {
                return a;
            }
            return 1;
        }

        public static double Power(int a, int n)
        {
            if (n > 2)
            {
                double r = Power(a, n / 2);
                if (n % 2 == 0)
                {
                    return r*r;
                }
                return a*r*r;
            }
            if (n == 2)
            {
                return a*a;
            }
            if (n == 1)
            {
                return a;
            }
            return 1;
        }

        public static decimal Power(decimal a, int n)
        {
            if (n > 2)
            {
                decimal r = Power(a, n / 2);
                if (n % 2 == 0)
                {
                    return r * r;
                }
                return a * r * r;
            }
            if (n == 2)
            {
                return a * a;
            }
            if (n == 1)
            {
                return a;
            }
            return 1;
        }

        public static double Power(double a, int n)
        {
            if (n > 2)
            {
                double r = Power(a, n / 2);
                if (n % 2 == 0)
                {
                    return r * r;
                }
                return a * r * r;
            }
            if (n == 2)
            {
                return a*a;
            }
            if (n == 1)
            {
                return a;
            }
            return 1;
        }

    }
}
